const deleteAndEarn = nums => {
  const rob = nums => {
    let length = nums.length
    if (length === 1) return nums[0]
    let sum1 = nums[0],
      sum2 = Math.max(nums[0], nums[1])
    for (let i = 2; i < length; i++) {
      [sum1, sum2] = [sum2, Math.max(sum1 + nums[i], sum2)]
    }
    return sum2
  }

  const n = nums.length
  let ans = 0
  nums.sort((a, b) => a - b)
  let total = [nums[0]]
  console.log(total);
  for (let i = 1; i < n; i++) {
    const val = nums[i]
    if (val === nums[i - 1]) {
      total[total.length - 1] += val
    } else if (val === nums[i - 1] + 1) {
      total.push(val)
    } else {
      ans += rob(total)
      total = [val]
    }
  }
  ans += rob(total)
  return ans
}


console.log(deleteAndEarn([3, 4, 2]));